<?php
// +----------------------------------------------------------------------
// | 后台菜单模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-21下午4:29:59
// +----------------------------------------------------------------------

defined('BASEPATH') OR exit('No direct script access allowed');
class Menu_model extends CI_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'menu';
	}
	
	/**
	 * 获取数据
	 * @param booter $isarr 返回结果集为对象还是数组
	 * @param unknown_type $where　获取条件
	 * @param unknown_type $field　获取字段
	 * @param unknown_type $order　自定义排序
	 * @param unknown_type $limit　获取记录数
	 */
	public function getData($isarr=1,$where=array(), $field="id,title,pid,sort,url,hide,group,is_dev", $order="id ASC", $limit=""){
		$res = $this->db->select($field)->where($where)->order_by($order)->limit($limit)->get($this->table_name);
		return $isarr == 0 ? $res->result() : $res->result_array();
	}
	/**
	 * 获取数据
	 * @param array $where 获取条件
	 * @param string $field 获取字段
	 * @author Baip
	 */
	public function getFind($isojb=1,$where=array(), $field="id,title,pid,sort,url,hide,group,is_dev", $order="id ASC", $limit="1"){
		$res = $this->db->select($field)->where($where)->order_by($order)->limit($limit)->get($this->table_name);
		return $isojb == 0 ? $res->row_array() : $res->row();
	}
	
	/**
	 * 获取树的根到子节点的路径
	 * @param unknown_type $id
	 * @author Baip
	 */
	public function getPath($id){
		$path = array();
		$res = $this->db->where("id",$id)->select('id,pid,title')->get($this->table_name);
		$nav = $res->row_array();
		$path[] = $nav;
		if($nav['pid'] >1){
			$path = array_merge($this->getPath($nav['pid']),$path);
		}
		return $path;
	}
	
	/**
	 * 去重获取数据
	 * @param array/string $where 获取条件
	 * @param unknown_type $field
	 */
	public function getDistinct($where, $field){
		$res = $this->db->distinct()->select($field)->where($where)->get($this->table_name);
		return $res->result_array();
	}
	/**
	 * 获取数据部数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 * @author Baip
	 */
	public function getCount($where=array(), $field="id"){
		$count =  $this->db->where($where)->select($field)->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 获取字段，然后根据传递的字段返回的数据为（前字段的值为下标后字段的值为值）
	 * @param array/string $where　读取条件 
	 * @param string/array $field 字段
	 */
	public function getField($where=array(), $field="id,title", $type=true){
		$res = $this->db->select($field)->where($where)->get( $this->table_name );
		$query = $res->result_array();
		$field = explode(",", $field);
		if ( $type ){
			$query = count( $field ) > 1 ? i_array_column($query, $field[1], $field[0]) : i_array_column($query, $field[0]);
		}
		return $query;
	}
	/**
	 * 添加数据
	 */
	public function addData(){
		$data_arr = $this->postData();
		$query = $this->db->insert($this->table_name, $data_arr);
		return $this->db->insert_id();
	}
	
	/**
	 * 更新数据
	 */
	public function updateDataByPostData($where){
		if ( empty( $where ) ){
			return false;
		}
		$data_arr = $this->postData();
		$query = $this->db->where($where)->update($this->table_name, $data_arr);
		return $query;
	}
	
	/**
	 * post接收值
	 */
	private function postData(){
		$title  = $this->input->post('title',true);//菜单名称
		$url  = $this->input->post('url',true);//菜单链接地址
		$pid  = $this->input->post('pid',true);//父级id
		$sort  = $this->input->post('sort',true);//自定义排序号
		$hide  = $this->input->post('hide',true);//是否隐藏
		$tip  = $this->input->post('tip',true);//菜单说明
		$group  = $this->input->post('group',true);//菜单分组
		$is_dev  = $this->input->post('is_dev',true);//是否为开发模式
		
		//产生随机字符串
		$data_arr['title'] = $title;
		$data_arr['pid'] = $pid;
		$data_arr['sort'] = $sort;
		$data_arr['url'] = $url;
		$data_arr['hide'] = $hide;
		$data_arr['tip'] = $tip;
		$data_arr['group'] = $group;
		$data_arr['is_dev'] = $is_dev;
		return $data_arr;
	}
	
	/**
	 * 更新数据
	 * @param string/array $map_value 操作条件值
	 * @param int $type 操作类型 1：对等，2：in_where...
	 * @param string $map_field : 操作条件字段
	 * @param array $data_arr 更新的数据
	 */
	public function updateData($map_value, $type="", $map_field="uid",$data_arr){
		switch ($type){
			case 1:
				$this->db->where(array($map_field=>$map_value));
				break;
			case 2:
				$this->db->where_in($map_field, $map_value);
				break;
		}
	
		$query = $this->db->update($this->table_name, $data_arr);
		return $query;
	}
	
	/**
	 * 删除数据
	 * @param unknown_type $map_value
	 * @param unknown_type $type
	 * @param unknown_type $map_field
	 */
	public function deleteData($field_val, $field="uid"){
		if ( empty( $field_val ) || empty( $field ) ){
			return false;
		}
		//获取当前操作条件的数据的pid()
		$count = $this->getCount(array('pid'=>$field_val),'*');
		if ( $count > 0 ){
			ajaxreturn(0,'当前菜单有子菜单不能删除', site_url('menu/index'));
		}
		//处理操作条件
		if ( is_array( $field_val ) ){
			$this->db->where_in($field, $field_val);
		}else{
			$this->db->where( array($field => $field_val));
		}
		
		$query = $this->db->delete($this->table_name);
		return $query;
	}
	
}